//
//  LivePreviewView.m
//  liveTest
//
//  Created by Aaron on 2016/12/20.
//  Copyright © 2016年 Zhu jinxin. All rights reserved.
//

#import "LivePreviewView.h"

#import "Masonry.h"

@interface LivePreviewView ()<LFLiveSessionDelegate>


@property (nonatomic, strong) UIButton * cameraChangeButton;
@property (nonatomic, strong) UILabel *stateLabel;

@end

@implementation LivePreviewView

- (instancetype)initWithFrame:(CGRect)frame{
    
    if ( self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor clearColor];
        
        [self addSubview:self.cameraChangeButton];
        [self addSubview:self.stateLabel];
        [self layoutSubViews];
        //默认后置摄像头
        self.session.captureDevicePosition = AVCaptureDevicePositionBack;
        
    }

    return self;
}

//布局页面
- (void)layoutSubViews{
    
    [_stateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(50));
        make.height.equalTo(@(30));
        make.left.equalTo(self).offset(10);
        make.top.equalTo(self).offset(20);
    }];
    
    [_cameraChangeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_stateLabel.mas_bottom).offset(15);
        make.right.equalTo(_stateLabel);
    }];
    
    
}

//更改摄像头方向
- (void)cameraChange{
    
    if (self.session.captureDevicePosition == AVCaptureDevicePositionBack)
    {
        self.session.captureDevicePosition = AVCaptureDevicePositionFront;
    }
    else
    {
        self.session.captureDevicePosition = AVCaptureDevicePositionBack;
    }
}

#pragma mark - 懒加载

//session
- (LFLiveSession *)session
{
    if (!_session)
    {
        _session = [[LFLiveSession alloc]initWithAudioConfiguration:[LFLiveAudioConfiguration defaultConfiguration] videoConfiguration:[LFLiveVideoConfiguration defaultConfigurationForQuality:LFLiveVideoQuality_Medium3]];
        
        _session.delegate = self;
        _session.showDebugInfo = YES;
        _session.preView = self;
        _session.running = YES;

        
    }
    return _session;
}


- (UIButton *)cameraChangeButton
{
    if (!_cameraChangeButton)
    {
        _cameraChangeButton = [[UIButton alloc] init];
        [_cameraChangeButton setImage:[UIImage imageNamed:@"camera_change_40x40_"] forState:UIControlStateNormal];
        [_cameraChangeButton sizeToFit];
        [_cameraChangeButton addTarget:self action:@selector(cameraChange) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cameraChangeButton;
}

- (UILabel *)stateLabel
{
    if (!_stateLabel)
    {
        _stateLabel = [[UILabel alloc] init];
        _stateLabel.textAlignment = NSTextAlignmentCenter;
        _stateLabel.font = [UIFont systemFontOfSize:12];
        _stateLabel.textColor = [UIColor whiteColor];
        _stateLabel.text = @"测试状态";
    }
    return _stateLabel;
}

#pragma mark - LFLiveSessionDelegate
/** live status changed will callback */
- (void)liveSession:(nullable LFLiveSession *)session liveStateDidChange:(LFLiveState)state
{
    NSLog(@"liveStateDidChange: %ld", (unsigned long)state);
    switch (state)
    {
        case LFLiveReady:
            _stateLabel.text = @"未连接";
            break;
        case LFLivePending:
            _stateLabel.text = @"连接中";
            break;
        case LFLiveStart:
            _stateLabel.text = @"已连接";
            break;
        case LFLiveError:
            _stateLabel.text = @"连接错误";
            break;
        case LFLiveStop:
            _stateLabel.text = @"未连接";
            break;
        default:
            break;
    }
}
/** live debug info callback */
- (void)liveSession:(nullable LFLiveSession *)session debugInfo:(nullable LFLiveDebug *)debugInfo
{
    
}
/** callback socket errorcode */
- (void)liveSession:(nullable LFLiveSession *)session errorCode:(LFLiveSocketErrorCode)errorCode
{
    
}

@end
